/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.click.app.webapp.action;

import com.click.app.Constants;
import com.click.app.DateFormatChanger;
import com.click.app.model.BalanceEmployee;
import com.click.app.model.Employee;
import com.click.app.model.Material;
import com.click.app.model.PantDetail;
import com.click.app.model.PeriodPaymentEmployee;
import com.click.app.model.ProcessDressmaker;
import com.click.app.model.ProcessDressmakerPurchaseDetail;
import com.click.app.model.StorageCut;
import com.click.app.model.StorageMaterial;
import com.click.app.service.BalanceEmployeeManager;
import com.click.app.service.CutDetailManager;
import com.click.app.service.EmployeeManager;
import com.click.app.service.PantDetailCutDetailManager;
import com.click.app.service.PantDetailManager;
import com.click.app.service.PeriodPaymentEmployeeManager;
import com.click.app.service.ProcessDressmakerManager;
import com.click.app.service.ProcessDressmakerPurchaseDetailManager;
import com.click.app.service.PurchaseDetailManager;
import com.click.app.service.StorageCutManager;
import com.click.app.service.StorageMaterialManager;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author ronald
 */

public class ProcessDressMakerAction extends BaseAction {
   private static final long serialVersionUID = -4037514607101222025L;

   ProcessDressmakerManager processDressmakerManager;
   PantDetailCutDetailManager pantDetailCutDetailManager;
   
   
   EmployeeManager employeeManager;
   CutDetailManager cutDetailManager;
   List<PantDetail> pantDetailList;
   ProcessDressmaker processDressmaker;

   List<ProcessDressmaker> processDressmakerList;
   List<Employee> employeesList;
   private Long id;

   private StorageCutManager storageCutManager;
   private List<StorageCut> storageCutList;
   private PurchaseDetailManager purchaseDetailManager;
   private List<StorageMaterial> storageMaterialList;
   private StorageMaterialManager storageMaterialManager;
   private List<ProcessDressmakerPurchaseDetail> listProcessDressmakerPurchaseDetail;
   private ProcessDressmakerPurchaseDetailManager processDressmakerPurchaseDetailManager;
   private ProcessDressmakerPurchaseDetail processDressmakerPurchaseDetail;
   private PantDetail pantDetail;
   private PantDetailManager pantDetailManager;

   private BalanceEmployeeManager balanceEmployeeManager;
   private String intDateProcessDMaker;
   private String endDateProcessDressmaker;

   private PeriodPaymentEmployeeManager periodPaymentEmployeeManager;
   private PeriodPaymentEmployee periodPaymentEmployee;

   public String list(){
       getSession().removeAttribute("pantDetailList");
       processDressmakerList = processDressmakerManager.getProcessDressmakers();
       return SUCCESS;
   }

   public String listReview(){
        processDressmakerList = processDressmakerManager.getProcessDressmakers();
        return SUCCESS;
   }
   public String save(){
    //  pantDetailList  =  (List<PantDetail>) getSession().getAttribute("pantDetailList");
      if(processDressmaker.getEmployee().getId() != -1 && intDateProcessDMaker != null){

          Employee employee = employeeManager.get(processDressmaker.getEmployee().getId());
          processDressmaker.setInitDate(DateFormatChanger.changeStringtoDate(intDateProcessDMaker));
          processDressmaker.setEmployee(employee);
          processDressmaker.setEndDate(processDressmaker.getInitDate());
          processDressmaker.setTotalAmount(0.0D);
          processDressmaker.setTotalAmountFail(0.0D);
          processDressmaker.setTotalCurrentAmount(0.0D);
          processDressmaker = processDressmakerManager.save(processDressmaker);

          periodPaymentEmployee = periodPaymentEmployeeManager.getCurrentPeriodByIdEmployeeAndRole(employee.getId()
                                              , employee.getRoleByName("ROLE_EMPLOYEE_DRESSMAKER").getIdRole());
          BalanceEmployee balanceEmployee  = new BalanceEmployee();
          balanceEmployee.setBalance(periodPaymentEmployee.getCurrentAmount());
          balanceEmployee.setAmountPaid(0.0D);
          balanceEmployee.setAmountWon(0.0D);
          balanceEmployee.setProcessDressmaker(processDressmaker);
          balanceEmployee.setDateBalance(processDressmaker.getInitDate());
          balanceEmployee.setPeriodPaymentEmployee(periodPaymentEmployee);
          balanceEmployee.setPaymentsCut(null);
          balanceEmployee.setPaymentsPant(null);
          balanceEmployee = balanceEmployeeManager.save(balanceEmployee);
          processDressmaker.setBalanceEmployee(balanceEmployee);
          balanceEmployeeManager.save(balanceEmployee);

          getSession().setAttribute("pDressMId",processDressmaker.getId());
          storageCutList = storageCutManager.getAllGreaterThanCero();
          saveMessage(getText("processDressMakerAction.saveProcessDressMaker"));
          return SUCCESS;

      }else{
        errorMessage(getText("processDressMakerAction.errorProcessDressMaker"));
        return ERROR;
      }
   }
   public String saveProcessDressmakerReview(){
      if(endDateProcessDressmaker != null ){
          ProcessDressmaker processDressmakerAux = processDressmakerManager.get(processDressmaker.getId());
          processDressmakerAux.setEndDate(DateFormatChanger.changeStringtoDate(endDateProcessDressmaker));
          processDressmakerManager.save(processDressmakerAux);
      }
      return INPUT;
   }
   public String cancelProcessDressmakerReview(){
       getSession().removeAttribute("pDressMId");
       return INPUT;
   }
   public String addMaterialUsedDressMaker(){
        System.out.println("addMaterialDressmakerUsed() ");
        List<Material> materials = purchaseDetailManager.findAllNoPackageDetail();
        storageMaterialList = new ArrayList<StorageMaterial>();
        for (Iterator<Material> it = materials.iterator(); it.hasNext();) {
            Material material = it.next();
            storageMaterialList.addAll(storageMaterialManager.findByIdMaterialNoCero(material.getId()));
        }
        processDressmaker = processDressmakerManager.get((Long)getSession().getAttribute("pDMid"));
        listProcessDressmakerPurchaseDetail = processDressmaker.getListProcessDressmakerPurchaseDetail();
        System.out.println("end addMaterialDressmakerUsed() ");
        return SUCCESS;
   }
   public String cancel(){
        getSession().removeAttribute("pantDetailList");
        
        return CANCEL;
   }
   public String edit() {
      // pantDetailList = (List<PantDetail>) getSession().getAttribute("pantDetailList");
       if(id != null ){
           processDressmaker = processDressmakerManager.get(id);
           pantDetailList = processDressmaker.getPantDetails();
           
       }else{
           if(pantDetailList == null)
                pantDetailList = new ArrayList<PantDetail>();
           processDressmaker = new ProcessDressmaker();
       }
       employeesList = employeeManager.findByRole(Constants.ROLE_EMPLOYEE_DRESSMAKER);
       getSession().setAttribute("processDressmaker",processDressmaker);
       getSession().setAttribute("pantDetailList", pantDetailList);

       return SUCCESS;
   }
   public String addPantDetailProcessDressmaker(){
       if(id==null)
           if(getSession().getAttribute("pDressMId") != null)
               id = (Long)getSession().getAttribute("pDressMId");
       
       processDressmaker = processDressmakerManager.get(id);
       storageCutList = storageCutManager.getAllGreaterThanCero();
       pantDetailList = processDressmaker.getPantDetails();
       getSession().setAttribute("pDressMId",processDressmaker.getId());
       return SUCCESS;
   }
   public String delete(){
       if(id != null){
            processDressmakerManager.remove(id);
       }
        return SUCCESS;
   }
   public String reasignPantDetailProcessDressmaker(){
       if(id != null){
            pantDetail = pantDetailManager.get(id);
            processDressmakerList = processDressmakerManager.getAll();

       }
       return SUCCESS;
   }
   public String saveReAsignationPantDetail(){
       if(processDressmaker.getId() > -1 ){
            pantDetail = pantDetailManager.get(pantDetail.getId());
            processDressmaker = processDressmakerManager.get(processDressmaker.getId());
            pantDetail.setProcessDressmaker(processDressmaker);
            pantDetailManager.save(pantDetail);
       }
       return INPUT;
   }
   public String editReview(){
       System.out.println("ProcessDressMakerAction-> editReview() ");
       if(id == null)
           if(getSession().getAttribute("pDMid") != null)
               id = (Long)getSession().getAttribute("pDMid");

       if(id != null ){
           processDressmaker = processDressmakerManager.get(id);
           endDateProcessDressmaker = DateFormatChanger.changeDatetoString(processDressmaker.getEndDate());
           pantDetailList = processDressmaker.getPantDetails();

           listProcessDressmakerPurchaseDetail = processDressmaker.getListProcessDressmakerPurchaseDetail();
           getSession().setAttribute("pDMid", processDressmaker.getId());
       }
       System.out.println("ProcessDressMakerAction-> end editReview() ");
       return SUCCESS;
   }
   public String editMaterialUsedProcessDressmaker(){
       System.out.println("editMaterialUsedProcessDressmaker() "+id);
       processDressmakerPurchaseDetail = processDressmakerPurchaseDetailManager.get(id);
       System.out.println("end editMaterialUsedProcessDressmaker() ");
       return SUCCESS;
   }
   
    public ProcessDressmaker getProcessDressmaker() {
        return processDressmaker;
    }

    public void setProcessDressmaker(ProcessDressmaker processDressmaker) {
        this.processDressmaker = processDressmaker;
    }

    public ProcessDressmakerManager getProcessDressmakerManager() {
        return processDressmakerManager;
    }

    public void setProcessDressmakerManager(ProcessDressmakerManager processDressmakerManager) {
        this.processDressmakerManager = processDressmakerManager;
    }

    public CutDetailManager getCutDetailManager() {
        return cutDetailManager;
    }

    public void setCutDetailManager(CutDetailManager cutDetailManager) {
        this.cutDetailManager = cutDetailManager;
    }

    public List<PantDetail> getPantDetailList() {
        return pantDetailList;
    }

    public void setPantDetailList(List<PantDetail> pantDetailList) {
        this.pantDetailList = pantDetailList;
    }

    public List<ProcessDressmaker> getProcessDressmakerList() {
        return processDressmakerList;
    }

    public void setProcessDressmakerList(List<ProcessDressmaker> processDressmakerList) {
        this.processDressmakerList = processDressmakerList;
    }

    public List<Employee> getEmployeesList() {
        return employeesList;
    }

    public void setEmployeesList(List<Employee> employeesList) {
        this.employeesList = employeesList;
    }

    public EmployeeManager getEmployeeManager() {
        return employeeManager;
    }

    public void setEmployeeManager(EmployeeManager employeeManager) {
        this.employeeManager = employeeManager;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public PantDetailCutDetailManager getPantDetailCutDetailManager() {
        return pantDetailCutDetailManager;
    }

    public void setPantDetailCutDetailManager(PantDetailCutDetailManager pantDetailCutDetailManager) {
        this.pantDetailCutDetailManager = pantDetailCutDetailManager;
    }

    public StorageCutManager getStorageCutManager() {
        return storageCutManager;
    }

    public void setStorageCutManager(StorageCutManager storageCutManager) {
        this.storageCutManager = storageCutManager;
    }

    public List<StorageCut> getStorageCutList() {

        return storageCutList;
    }

    public void setStorageCutList(List<StorageCut> storageCutList) {
        this.storageCutList = storageCutList;
    }

    public PurchaseDetailManager getPurchaseDetailManager() {
        return purchaseDetailManager;
    }

    public void setPurchaseDetailManager(PurchaseDetailManager purchaseDetailManager) {
        this.purchaseDetailManager = purchaseDetailManager;
    }

    public List<StorageMaterial> getStorageMaterialList() {
        return storageMaterialList;
    }

    public void setStorageMaterialList(List<StorageMaterial> storageMaterialList) {
        this.storageMaterialList = storageMaterialList;
    }

    public StorageMaterialManager getStorageMaterialManager() {
        return storageMaterialManager;
    }

    public void setStorageMaterialManager(StorageMaterialManager storageMaterialManager) {
        this.storageMaterialManager = storageMaterialManager;
    }

    public List<ProcessDressmakerPurchaseDetail> getListProcessDressmakerPurchaseDetail() {
        return listProcessDressmakerPurchaseDetail;
    }

    public void setListProcessDressmakerPurchaseDetail(List<ProcessDressmakerPurchaseDetail> listProcessDressmakerPurchaseDetail) {
        this.listProcessDressmakerPurchaseDetail = listProcessDressmakerPurchaseDetail;
    }

    public PantDetail getPantDetail() {
        return pantDetail;
    }

    public void setPantDetail(PantDetail pantDetail) {
        this.pantDetail = pantDetail;
    }

    public PantDetailManager getPantDetailManager() {
        return pantDetailManager;
    }

    public void setPantDetailManager(PantDetailManager pantDetailManager) {
        this.pantDetailManager = pantDetailManager;
    }

    public ProcessDressmakerPurchaseDetailManager getProcessDressmakerPurchaseDetailManager() {
        return processDressmakerPurchaseDetailManager;
    }

    public void setProcessDressmakerPurchaseDetailManager(ProcessDressmakerPurchaseDetailManager processDressmakerPurchaseDetailManager) {
        this.processDressmakerPurchaseDetailManager = processDressmakerPurchaseDetailManager;
    }

    public ProcessDressmakerPurchaseDetail getProcessDressmakerPurchaseDetail() {
        return processDressmakerPurchaseDetail;
    }

    public void setProcessDressmakerPurchaseDetail(ProcessDressmakerPurchaseDetail processDressmakerPurchaseDetail) {
        this.processDressmakerPurchaseDetail = processDressmakerPurchaseDetail;
    }

    public BalanceEmployeeManager getBalanceEmployeeManager() {
        return balanceEmployeeManager;
    }

    public void setBalanceEmployeeManager(BalanceEmployeeManager balanceEmployeeManager) {
        this.balanceEmployeeManager = balanceEmployeeManager;
    }

    public String getIntDateProcessDMaker() {
        return intDateProcessDMaker;
    }

    public void setIntDateProcessDMaker(String intDateProcessDMaker) {
        this.intDateProcessDMaker = intDateProcessDMaker;
    }

    public String getEndDateProcessDressmaker() {
        return endDateProcessDressmaker;
    }

    public void setEndDateProcessDressmaker(String endDateProcessDressmaker) {
        this.endDateProcessDressmaker = endDateProcessDressmaker;
    }

    public PeriodPaymentEmployee getPeriodPaymentEmployee() {
        return periodPaymentEmployee;
    }

    public void setPeriodPaymentEmployee(PeriodPaymentEmployee periodPaymentEmployee) {
        this.periodPaymentEmployee = periodPaymentEmployee;
    }

    public PeriodPaymentEmployeeManager getPeriodPaymentEmployeeManager() {
        return periodPaymentEmployeeManager;
    }

    public void setPeriodPaymentEmployeeManager(PeriodPaymentEmployeeManager periodPaymentEmployeeManager) {
        this.periodPaymentEmployeeManager = periodPaymentEmployeeManager;
    }

}
